import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/manager/xindong_chat_singleton.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/conversation/input/xindong_liaotian_input_controller.dart';
import 'package:xindong/routes/conversation/input/xindong_liaotianInputSound.dart';
import 'package:xindong/routes/conversation/input/xindong_liaotian_keyboard_controller.dart';
import 'package:xindong/routes/conversation/xindong_liaotian_words_yemian.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

class XinDongLiaoTianInputTextVoice extends StatefulWidget {
  final String userId;
  final V2TimConversation conversation;

  const XinDongLiaoTianInputTextVoice({super.key, required this.userId, required this.conversation});

  @override
  State<XinDongLiaoTianInputTextVoice> createState() => _XinDongLiaoTianInputTextVoiceState();
}

class _XinDongLiaoTianInputTextVoiceState extends State<XinDongLiaoTianInputTextVoice> {
  final FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      //输入框焦点
      _focusNode.addListener(() {
        if (_focusNode.hasFocus) {
          XinDongLiaoTianInputController.to.close();
          XinDongLiaoTianInputController.to.closeMiniGames();
          XinDongLiaoTianInputController.to.upKeyboard(true);
        } else {
          XinDongLiaoTianInputController.to.upKeyboard(false);
        }
      });
    });
  }

  void onSubmit(String? text, context) {
    if (text == '' || text == null) {
      return;
    }
    // 不用await才清除inputController
    XinDongChatSingleton.instance.requestSendText(text: text);
    XinDongLiaoTianInputController.to.inputController.text = "";
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<XinDongLiaoTianKeyBoardController>(builder: (provider) {
      return Expanded(
        child: provider.show
            ? Container(
                constraints: const BoxConstraints(maxHeight: 88, minHeight: 48),
                decoration: BoxDecoration(color: DZColors.backgroundColor, borderRadius: BorderRadius.circular(16)),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Expanded(
                      child: Container(
                        constraints: const BoxConstraints(minHeight: 48 - (8 / 2)),
                        child: GetBuilder<XinDongLiaoTianInputController>(builder: (provider) {
                          return TextField(
                            focusNode: _focusNode,
                            controller: provider.inputController,
                            onSubmitted: Platform.isIOS ? (text) => onSubmit(text, context) : null,
                            autocorrect: false,
                            textAlign: TextAlign.left,
                            textAlignVertical: TextAlignVertical.center,
                            keyboardType: TextInputType.multiline,
                            textInputAction: Platform.isIOS ? TextInputAction.send : TextInputAction.unspecified,
                            decoration: const InputDecoration(
                              hintText: '说点什么',
                              hintStyle: TextStyle(color: DZColors.placeholderColor),
                              border: InputBorder.none,
                              isCollapsed: true,
                              isDense: true,
                              contentPadding: EdgeInsets.fromLTRB(16, 8, 0, 8),
                            ),
                            style: const TextStyle(fontSize: 16, color: DZColors.titleColor),
                            // minLines: 1,
                            maxLines: null,
                          );
                        }),
                      ),
                    ),
                    if (XinDongUserController.to.userInfo?.isFemale == true)
                      Container(
                        alignment: Alignment.centerRight,
                        height: 48,
                        width: 40,
                        child: Theme(
                          data: ThemeData(
                            highlightColor: DZColors.mainLightColor,
                            splashColor: DZColors.mainLightColor,
                          ),
                          child: IconButton(
                            icon: const Icon(Icons.more_horiz_rounded),
                            alignment: Alignment.centerRight,
                            padding: EdgeInsets.zero,
                            iconSize: 24.0,
                            onPressed: () {
                              FocusScope.of(context).requestFocus(FocusNode());
                              XinDongLiaoTianInputController.to.closeMiniGames();
                              XinDongPageSingleton.instance.pushYeMian(const XinDongLiaoTianWordsYeMian());
                            },
                          ),
                        ),
                      ),
                    Container(
                      alignment: Alignment.center,
                      height: 48,
                      child: Theme(
                        data: ThemeData(
                          highlightColor: DZColors.mainLightColor,
                          splashColor: DZColors.mainLightColor,
                        ),
                        child: IconButton(
                          icon: ImageUtil.asset(AssetsRes.CHAT_EMOJI),
                          padding: EdgeInsets.zero,
                          iconSize: 30.0,
                          onPressed: () {
                            FocusScope.of(context).requestFocus(FocusNode());
                            XinDongLiaoTianInputController.to.closeMiniGames();
                            bool character = XinDongLiaoTianInputController.to.onUpSpring();
                            if (character) {
                              XinDongLiaoTianKeyBoardController.to.showKeyboard();
                            }
                          },
                        ),
                      ),
                    ),
                    GetBuilder<XinDongLiaoTianInputController>(builder: (provider) {
                      if (TextUtil.isEmpty(provider.inputController.text)) {
                        return const SizedBox();
                      }
                      return Container(
                        width: 64,
                        margin: const EdgeInsets.only(right: 8),
                        padding: const EdgeInsets.only(bottom: 8, top: 8),
                        child: MaterialButton(
                          onPressed: () => onSubmit(provider.inputController.text, context),
                          height: 32.0,
                          textColor: Colors.black,
                          elevation: 0,
                          disabledTextColor: Colors.white,
                          color: DZColors.mainColor,
                          disabledColor: DZColors.hexStringColor("#000000", alpha: 0.6),
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(32.0)),
                          materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                          child: const Text(
                            "发送",
                            style: TextStyle(fontSize: 14, color: DZColors.white, height: 1.2),
                          ),
                        ),
                      );
                    })
                  ],
                ),
              )
            : XinDongLiaoTianInputSound(
                send: (path, duration) {
                  XinDongChatSingleton.instance.requestSendVoice(filePath: path, second: duration);
                },
              ),
      );
    });
  }
}
